import 'dart:convert';

import 'package:flutter_provider_demo/commons/log.dart';
import 'package:flutter_provider_demo/commons/security.dart';
import 'package:flutter_provider_demo/config/environment.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  test('log level', () {
    var log = Log();
    log.debug('debug');
    log.info('info');
    log.warn('warning');
    log.error('error');
    log.fatal('fatal');
  });

  test('decode jwt token', () {
    var log = Log();
    Env.init();

    var token =
        "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3Mzc2ODQ5NDIsInVzZXJuYW1lIjoiYWRtaW4ifQ.JHdgZN5NbXL0Lq4RFkHOIUe1YPHe5-2sVx7p7MjSe3U";
    var parts = token.split('.');
    if (parts.length == 3) {
      var payload = parts[1];

      var payloadData = jsonDecode(SecurityUtils.base64Decode(payload)!);
      int expireTime = payloadData['exp'] as int;
      expireTime *= 1000;
      var now = DateTime.now().millisecondsSinceEpoch;
      var minutes = (now - expireTime) ~/ 60000;
      log.debug('now: $now, expireTime: $expireTime, minutes: $minutes');
      if (now > expireTime) {
        log.warn('token expired');
      }
    }
  });
}
